package com.ibk.system.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

public class Login {
	public static String login(String id, String pass) {
		String url = "http://192.168.0.74:8080/chap06_07_springMVC/login.do";
		HttpURLConnection http = null;
		BufferedReader bufferedReader = null;
		StringBuilder response = null;
		try {
			String data = URLEncoder.encode("id", "UTF-8") + "="
					+ URLEncoder.encode(id, "UTF-8") + "&"
					+ URLEncoder.encode("pass", "UTF-8") + "="
					+ URLEncoder.encode(pass, "UTF-8");
			
			http = (HttpURLConnection) new URL(url).openConnection();
			http.setDoOutput(true);
			http.setDoInput(true);
			http.setUseCaches(false);
			http.setRequestMethod("POST");
			http.setRequestProperty("connection", "close");
			http.connect();

			OutputStreamWriter os = new OutputStreamWriter(
					http.getOutputStream(), "UTF-8");
			os.write(data);
			os.flush();
			os.close();

			bufferedReader = new BufferedReader(new InputStreamReader(
					http.getInputStream()));
			String responeLine;

			response = new StringBuilder();

			while ((responeLine = bufferedReader.readLine()) != null) {
				response.append(responeLine);
			}
		} catch (Exception e) {
		} finally {
			try {
				if (bufferedReader != null) {
					bufferedReader.close();
				}
				http.disconnect();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		if (response == null) {
			return "";
		} else {
			return response.toString();
		}
	}
}
